package com.aliyun.code83.round2.beans;


import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class RadixTreeForSearch {

    private static final String ROOT_PATH = "";

    RadixNode rootNode;

    public RadixTreeForSearch() {
        this.rootNode = new RadixNode(ROOT_PATH, Boolean.TRUE);
    }

    public void addNode(String path, Boolean value) {
        addNode(this.rootNode, path, value);
    }

    public void addNode(RadixNode parent, String path, Boolean value) {

        int matchings = 0;
        while (matchings < path.length() && matchings < parent.path.length()) {
            if (path.charAt(matchings) != parent.path.charAt(matchings)) break;
            matchings++;
        }
        if (matchings > 0 && matchings < parent.path.length()) {
            if (Boolean.TRUE.equals(value)) {
                this.addToParent(parent, path, true, matchings);
            }
        } else {
            if (matchings == path.length()) {
                parent.wordValue = true;
                return;
            }
//                throw new Exception("Dumplicate trail:" + path);
            // 没有匹配或者超过/user -> /usersomething
            String newPath = path.substring(matchings);
            boolean foundInChildren = this.addToChild(parent, newPath, value);
            if (!foundInChildren) {
                if (ROOT_PATH.equals(parent.path) && Boolean.FALSE.equals(value)) {
                    return;
                }
                RadixNode node = new RadixNode(newPath, value);
                parent.children.add(node);
            }
        }
    }

    private boolean addToChild(RadixNode parent, String newPath, Boolean value) {
        char first = newPath.charAt(0);
        boolean foundChild = false;
        for (RadixNode child : parent.children) {
            if (child.path.startsWith(first + "")) {
                foundChild = true;
                addNode(child, newPath, value);
                break;
            }
        }
        return foundChild;
    }

    private void addToParent(RadixNode parent, String path, Boolean value, int matchings) {
        String subpath = parent.path.substring(matchings);
        RadixNode node = new RadixNode(subpath, parent.nodeValue);
        node.children = parent.children;
        parent.path = path.substring(0, matchings);
        parent.children = new LinkedList<>();
        parent.children.add(node);
        if (matchings < path.length()) {
            parent.nodeValue = null;
            String subpath2 = path.substring(matchings);
            RadixNode node2 = new RadixNode(subpath2, value);
            parent.children.add(node2);
        } else {
            parent.nodeValue = value;
        }
    }


    public Map<String, List<String>> searchAll() {
        Map<String, List<String>> result = new HashMap<>();
        searchAll(rootNode, ROOT_PATH, result);
        return result;
    }

    public void searchAll(RadixNode rootNode, String prefix, Map<String, List<String>> result) {
        if (!ROOT_PATH.equals(rootNode.path)) {
            if (Boolean.TRUE.equals(rootNode.nodeValue)) {
                result.put(prefix, new LinkedList<>());
                rootNode.keys.add(prefix);
            }
            if (Boolean.TRUE.equals(rootNode.wordValue)) {
                for (String key : rootNode.keys) {
                    result.get(key).add(prefix);
                }
            }
        }
        for (RadixNode child : rootNode.children) {
            child.keys.addAll(rootNode.keys);
            searchAll(child, prefix + child.path, result);
        }
    }


    public void print() {
        printNode("", this.rootNode, 0, 1);
    }

    private void printNode(String prefix, RadixNode node, int index, int total) {
        String a = "└";
        if (total > 1 && index != total - 1) a = "├";
        String line = String.format("%-50s<%s>", prefix + a + node.path, node.nodeValue);
        System.out.println(line);
        int padding = node.path.length();
        if (total > 1 && index != total - 1) {
            prefix += "│";
            padding--;
        }
        for (int i = 0; i < padding; i++)
            prefix += " ";

        int size = node.children.size();
        for (int j = 0; j < size; j++) {
            RadixNode child = node.children.get(j);
            printNode(prefix, child, j, size);
        }
    }


    class RadixNode {

        String path;

        Boolean nodeValue = false;

        Boolean wordValue = false;

        List<String> keys = new LinkedList<>();

        List<RadixNode> children = new LinkedList<>();

        public RadixNode(String path, Boolean value) {
            this.path = path;
            if (Boolean.TRUE.equals(value)) {
                this.nodeValue = true;
            } else {
                this.wordValue = true;
            }
        }
    }


}

